/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sale;

/**
 *
 * @author Yasmani
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import search.Product;
import java.sql.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class InsertSell 
{
   private static final String HOST = "localhost";
   private static final String PORT = "3306";
   private static final String DATABASE = "ismi";
   private static final String USER = "root";
   private static final String PASSWORD = "isaac16";
   public String username;
   private Connection connection;
   
   public InsertSell() {}
   
    
   public void connect()
   {
      try
      {
          
         // Override the host if DB_SERVER_HOST is set.
         String host = System.getenv("DB_SERVER_HOST");
         if (host == null) host = HOST;

         Class.forName("com.mysql.jdbc.Driver");

         String url =
            "jdbc:mysql://" + host + ":" + PORT + "/" + DATABASE;
         connection =
            DriverManager.getConnection(url, USER, PASSWORD);
      }
      catch (Exception e) { System.err.println(e); }
   }

   public void disconnect()
   {
      try { connection.close(); }
      catch (Exception e) { System.err.println(e); }
   }
   
           
public void SaleProd(String product_name, String product_description, String price, String amount, String user_name,String partNumber, String manufacturer,String pic)
   {
       Product saleProduct= new Product(product_name, product_description, price, amount, user_name, partNumber, manufacturer,pic);
       System.out.println(user_name);
       System.out.println(partNumber);
       try{
     
       PreparedStatement statement =  connection.prepareStatement("INSERT INTO `ismi`.`product` (product_name, product_description, product_price, product_amount,user_name, part_number,manufacturer, product_picture)"
               + " VALUES (?,?, ?, ?,?, ?, ?,?);");
       
           //statement.setIn(1, );
           statement.setString(1, saleProduct.getProduct_name());
           statement.setString(2, saleProduct.getProduct_description());
           statement.setFloat(3, Float.parseFloat( saleProduct.getPrice()));
           statement.setInt(4, Integer.parseInt(saleProduct.getAmount()));
           statement.setString(5, saleProduct.getUser_name());
           statement.setString(6, saleProduct.getPartNumber());
           statement.setString(7, saleProduct.getManufacturer());
           statement.setString(8,saleProduct.getPic());
           statement.executeUpdate();
           
        } 
       
       catch (Exception e) {e.printStackTrace();}
       
   }
}